function C57_Midazolam_Ca_Event_Rate_Dist_Plot(Data_Summary)
%% Get saline Ca Event Rate Distributions ready for plotting
for n=1:size(Data_Summary,2)
    if strcmp(Data_Summary(n).drug, "saline")
        Saline_Ca_Rate(n,:)=Data_Summary(n).BinCounts_Session1_Event_Rate_Proportion;
    end
end
for n=1:size(Saline_Ca_Rate,1)
    if max(Saline_Ca_Rate(n,:))==0
        Saline_Ca_Rate(n,:)=nan;
    end
end

for n=1:size(Saline_Ca_Rate,2)
    Saline_Ca_Rate_Mean(1,n)=mean(Saline_Ca_Rate(:,n),'omitnan');
end

for n=1:size(Saline_Ca_Rate,2)
    Saline_Ca_Rate_Err(1,n)=calculate_standard_error(Saline_Ca_Rate(:,n));
end

%% Get MIDA 0.25mg/kg Ca Event Rate Distributions ready for plotting
for n=1:size(Data_Summary,2)
    if strcmp(Data_Summary(n).drug, "midazolam 0.25mg/kg")
        MIDA025_Ca_Rate(n,:)=Data_Summary(n).BinCounts_Session1_Event_Rate_Proportion;
    end
end
for n=1:size(MIDA025_Ca_Rate,1)
    if max(MIDA025_Ca_Rate(n,:))==0
        MIDA025_Ca_Rate(n,:)=nan;
    end
end


for n=1:size(MIDA025_Ca_Rate,2)
    MIDA025_Ca_Rate_Mean(1,n)=mean(MIDA025_Ca_Rate(:,n),'omitnan');
end

for n=1:size(MIDA025_Ca_Rate,2)
    MIDA025_Ca_Rate_Err(1,n)=calculate_standard_error(MIDA025_Ca_Rate(:,n));
end

%% Get MIDA 1.25mg/kg Ca Event Rate Distributions ready for plotting
for n=1:size(Data_Summary,2)
    if strcmp(Data_Summary(n).drug, "midazolam 1.25mg/kg")
        MIDA125_Ca_Rate(n,:)=Data_Summary(n).BinCounts_Session1_Event_Rate_Proportion;
    end
end
for n=1:size(MIDA125_Ca_Rate,1)
    if max(MIDA125_Ca_Rate(n,:))==0
        MIDA125_Ca_Rate(n,:)=nan;
    end
end


for n=1:size(MIDA125_Ca_Rate,2)
    MIDA125_Ca_Rate_Mean(1,n)=mean(MIDA125_Ca_Rate(:,n),'omitnan');
end

for n=1:size(MIDA125_Ca_Rate,2)
    MIDA125_Ca_Rate_Err(1,n)=calculate_standard_error(MIDA125_Ca_Rate(:,n));
end

%% Get MIDA 2.5mg/kg Ca Event Rate Distributions ready for plotting
for n=1:size(Data_Summary,2)
    if strcmp(Data_Summary(n).drug, "midazolam 2.5mg/kg")
        MIDA25_Ca_Rate(n,:)=Data_Summary(n).BinCounts_Session1_Event_Rate_Proportion;
    end
end
for n=1:size(MIDA25_Ca_Rate,1)
    if max(MIDA25_Ca_Rate(n,:))==0
        MIDA25_Ca_Rate(n,:)=nan;
    end
end


for n=1:size(MIDA25_Ca_Rate,2)
    MIDA25_Ca_Rate_Mean(1,n)=mean(MIDA25_Ca_Rate(:,n),'omitnan');
end

for n=1:size(MIDA25_Ca_Rate,2)
    MIDA25_Ca_Rate_Err(1,n)=calculate_standard_error(MIDA25_Ca_Rate(:,n));
end



%% plotting Ca Event Rate Distributions distribution
% define boundary
Upper_Bound=0.5;
Lower_Bound=0; 
Interval=0.02; 

figure
subplot(1,4,1)
bar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),Saline_Ca_Rate_Mean,1,'b','FaceAlpha',0.6);
ylim([0 0.2]);
xlim([0 0.5]);
xlabel('Calcium Event Rate (spikes/sec)');
ylabel('Fraction of Cells');
hold on
er = errorbar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),Saline_Ca_Rate_Mean,[],Saline_Ca_Rate_Err,'LineWidth',0.8);    
er.Color = [0 0 0];                            
er.LineStyle = 'None'; 
set(gca,'FontSize',15);
hold off
title('Saline','FontSize',15);

subplot(1,4,2)
bar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),MIDA025_Ca_Rate_Mean,1,'r','FaceAlpha',0.2);
ylim([0 0.2]);
xlim([0 0.5]);
xlabel('Calcium Event Rate (spikes/sec)');
ylabel('Fraction of Cells');
hold on
er = errorbar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),MIDA025_Ca_Rate_Mean,[],MIDA025_Ca_Rate_Err,'LineWidth',0.8);    
er.Color = [0 0 0];                            
er.LineStyle = 'None'; 
set(gca,'FontSize',15);
hold off
title('Saline','FontSize',15);

subplot(1,4,3)
bar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),MIDA125_Ca_Rate_Mean,1,'r','FaceAlpha',0.4);
ylim([0 0.2]);
xlim([0 0.5]);
% xlabel('Calcium Event Rate (spikes/sec)');
% ylabel('Fraction of Cells');
hold on
er = errorbar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),MIDA125_Ca_Rate_Mean,[],MIDA125_Ca_Rate_Err,'LineWidth',0.8);    
er.Color = [0 0 0];                            
er.LineStyle = 'None'; 
set(gca,'FontSize',15);
hold off
title('Midazolam 1.25mg/kg','FontSize',15);

subplot(1,4,4)
bar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),MIDA25_Ca_Rate_Mean,1,'r','FaceAlpha',0.6);
ylim([0 0.2]);
xlim([0 0.5]);
% xlabel('Calcium Event Rate (spikes/sec)');
% ylabel('Fraction of Cells');
hold on
er = errorbar((Lower_Bound+0.5*Interval:Interval:Upper_Bound),MIDA25_Ca_Rate_Mean,[],MIDA25_Ca_Rate_Err,'LineWidth',0.8);    
er.Color = [0 0 0];                            
er.LineStyle = 'None'; 
set(gca,'FontSize',15);
hold off
title('Midazolam 2.5mg/kg','FontSize',15);


end